package com.mc.code.web.sys;

import com.mc.code.entity.sys.SysItems;
import com.mc.code.sysService.SysItemsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 项目管理
 */
@Controller
public class ItemsManageController {

    //分页
    private static final long serialVersionUID = 1L;
    private static final int PAGE_COUNT = 3;//每页显示记录数

    @Autowired
    private SysItemsService sysItemsService;

    //模糊查询项目列表
    @RequestMapping("/itemsList")
    public String queryItemsList(String itemType, String itemCondition, String itemDid, Model model, HttpServletRequest request){
        //默认当前页
        int currentPage = 1;
        String page = request.getParameter("page");
        if(page != null) {
            currentPage = Integer.valueOf(page);
        }
        List<SysItems> sysItemsList = sysItemsService.queryItems(itemType, itemCondition, itemDid,currentPage,PAGE_COUNT);
        int totalPage = sysItemsService.getTotalPage(PAGE_COUNT,itemType,itemCondition,itemDid);
        model.addAttribute("totalPage",totalPage);
        model.addAttribute("currentPage", currentPage);
        model.addAttribute("sysItemsList",sysItemsList);
        model.addAttribute("itemType",itemType);
        model.addAttribute("itemCondition",itemCondition);
        model.addAttribute("itemDid",itemDid);
        return "sys/itemsManage" ;
    }

    //项目详情
    @RequestMapping("/itemsDetail")
    public String itemsDetail(Model model, int itemNo){
        SysItems sysItems = sysItemsService.findByItemNo(itemNo);
        model.addAttribute("sysItems",sysItems);
        return "sys/itemsDetail";
    }

    //审核项目
    @RequestMapping("/checkItems")
    public String chekcItesm(Model model, int itemNo){
        SysItems sysItems = sysItemsService.findByItemNo(itemNo);
        model.addAttribute("items", sysItems);
        return "sys/checkItems";
    }

    //项目通过
    @RequestMapping("/itemsPass")
    public String itemsPass(int itemNo, HttpServletResponse response)throws IOException{
        sysItemsService.passItems(itemNo);
        response.getWriter().print("success");
        return null;
    }

    //项目不通过
    @RequestMapping("/itemsNoPass")
    public String itemsNoPass(int itemNo, HttpServletResponse response)throws IOException {
        sysItemsService.noPassItems(itemNo);
        response.getWriter().print("success");
        return null;
    }
}
